package com.github.hongkaiwen.rxjava.play.problem1.play;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class FuturePlay2 {

    public static void main(String args[]){
        //调用d，基于d的结果，调用f
        ExecutorService service = Executors.newCachedThreadPool();
        Future<Integer> f1 = service.submit(() -> d());
        Observable.fromFuture(f1).subscribeOn(Schedulers.io())
            .flatMap(t -> Observable.fromFuture(service.submit(() -> f(t))))
            .subscribe(System.out::println);

        System.out.println("do other things");


    }

    private static int d() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        return 2;
    }

    private static int f(int input) throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        return input + 4;
    }

}
